// 将 HEX 转换为 RGB
export const hexToRgb = (hex: any) => {
  const r = parseInt(hex.slice(1, 3), 16);
  const g = parseInt(hex.slice(3, 5), 16);
  const b = parseInt(hex.slice(5, 7), 16);
  return { r, g, b };
};

// 将 RGB 转换为 HEX
export const rgbToHex = (r: any, g: any, b: any) => {
  return `#${((1 << 24) | (r << 16) | (g << 8) | b).toString(16).slice(1).toUpperCase()}`;
};

// 增加亮度或减少亮度
export const adjustBrightness = (rgb: any, factor: any) => {
  return {
    r: Math.min(255, rgb.r * factor),
    g: Math.min(255, rgb.g * factor),
    b: Math.min(255, rgb.b * factor)
  };
};

// 根据主题色计算渐变的起始色和结束色
export const generateGradient = (primaryColor: any) => {
  const rgb = hexToRgb(primaryColor);

  // 假设增加亮度和减少亮度来生成渐变的结束颜色
  const lighterColor = adjustBrightness(rgb, 1.2); // 增加亮度
  const darkerColor = adjustBrightness(rgb, 6); // 减少亮度

  const lighterHex = rgbToHex(lighterColor.r, lighterColor.g, lighterColor.b);
  const darkerHex = rgbToHex(darkerColor.r, darkerColor.g, darkerColor.b);

  // 返回一个渐变的 CSS 样式
  return `linear-gradient(90deg, ${primaryColor}, ${lighterHex}, ${darkerHex})`;
};

// 计算稍微淡一点的颜色
export const getLighterColor = (hexColor: string) => {
  const rgb = hexToRgb(hexColor);
  const lighterRgb = adjustBrightness(rgb, 0.8); // 减少亮度（80%）
  return rgbToHex(lighterRgb.r, lighterRgb.g, lighterRgb.b);
};

// 工具函数：生成较亮的颜色
export const lightenColor = (hex: any, ratio: any) => {
  const { r, g, b } = hexToRgb(hex);
  const lighten = (value: any) => Math.min(255, Math.round(value + (255 - value) * ratio));
  return `rgb(${lighten(r)}, ${lighten(g)}, ${lighten(b)})`;
};

export const hslToHex = (h: number, s: number, l: number) => {
  s /= 100;
  l /= 100;

  const c = (1 - Math.abs(2 * l - 1)) * s;
  const x = c * (1 - Math.abs(((h / 60) % 2) - 1));
  const m = l - c / 2;

  let r = 0,
    g = 0,
    b = 0;

  if (h >= 0 && h < 60) [r, g, b] = [c, x, 0];
  else if (h >= 60 && h < 120) [r, g, b] = [x, c, 0];
  else if (h >= 120 && h < 180) [r, g, b] = [0, c, x];
  else if (h >= 180 && h < 240) [r, g, b] = [0, x, c];
  else if (h >= 240 && h < 300) [r, g, b] = [x, 0, c];
  else if (h >= 300 && h < 360) [r, g, b] = [c, 0, x];

  r = Math.round((r + m) * 255);
  g = Math.round((g + m) * 255);
  b = Math.round((b + m) * 255);

  return `#${((1 << 24) | (r << 16) | (g << 8) | b).toString(16).slice(1)}`;
};

export const hexToHSL = (hex: string) => {
  let r = parseInt(hex.substring(1, 3), 16) / 255;
  let g = parseInt(hex.substring(3, 5), 16) / 255;
  let b = parseInt(hex.substring(5, 7), 16) / 255;

  let max = Math.max(r, g, b),
    min = Math.min(r, g, b);
  let h = 0,
    s = 0,
    l = (max + min) / 2;

  if (max !== min) {
    let d = max - min;
    s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
    if (max === r) h = (g - b) / d + (g < b ? 6 : 0);
    else if (max === g) h = (b - r) / d + 2;
    else if (max === b) h = (r - g) / d + 4;
    h *= 60;
  }

  return { h: Math.round(h), s: Math.round(s * 100), l: Math.round(l * 100) };
};

export const adjustLightness = (hex: string, percent: number) => {
  const hsl = hexToHSL(hex);
  hsl.l = Math.min(100, Math.max(0, hsl.l + percent)); // 限制 0~100
  return hslToHex(hsl.h, hsl.s, hsl.l);
};

export const circleThemeColor = (themeColor: string) => {
  const lightColor = adjustLightness(themeColor, 34); // #80cbc4
  const darkColor = adjustLightness(themeColor, -6); // #00796b
  // 返回一个渐变的 CSS 样式
  return `linear-gradient(90deg, ${lightColor}, ${themeColor}, ${darkColor})`;
};
